<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('岗位管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>岗位名称：</label>
                                <input type="text" name="postName"/>
                            </li>
                            <li hidden>
                                <label>岗位发布者：</label>
                                <input type="text" name="createUserId" th:value="${user.UserId}"/>
                            </li>
                            <li hidden>
                                <label>岗位状态：</label>
                                <input type="text" name="params[postStatus]" value="WAIT_FOR_PUBLISH"/>
                            </li>
                            <li hidden>
                                <label>岗位状态：</label>
                                <input type="text" name="params[postStatus2]" value="IN_HANDING"/>
                            </li>
                            <li>
                                <label>开始：</label>
                                <input type="text" class="time-input" placeholder="请选择招聘开始时间" name="startTime"/>
                            </li>
                            <li>
                                <label>结束时间：</label>
                                <input type="text" class="time-input" placeholder="请选择招聘结束时间" name="endTime"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="wstudy:post:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="wstudy:post:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="wstudy:post:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="wstudy:post:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-primary single disabled" onclick="postFinished()" shiro:hasPermission="wstudy:post:edit">
                    <i class="fa fa-edit"></i> 完结
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('wstudy:post:edit')}]];
        var removeFlag = [[${@permission.hasPermi('wstudy:post:remove')}]];
        var postStatusDatas = [[${@dict.getType('wst_post_status')}]];
        var statusDatas = [[${@dict.getType('wst_post_status')}]];
        var prefix = ctx + "wstudy/post";
        var prefixStu = ctx + "wstudy/student";
        var prefixRecord = ctx + "wstudy/record";
        var prefixProcess = ctx + "wstudy/process";
        var prefixPE = ctx + "wstudy/examine";
        var roleKey = "administer";
        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "岗位管理",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'postId',
                    title: 'ID',
                    visible: false
                },
                {
                    field: 'postCode',
                    title: '岗位编码',
                    visible: false
                },
                {
                    field: 'postName',
                    title: '岗位名称'
                },
                {
                    field: 'needNum',
                    title: '招聘人数'
                },
                {
                    field: 'salary',
                    title: '工资'
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'workplace',
                    title: '工作地址'
                },
                {
                    field: 'startTime',
                    title: '招聘开始时间'
                },
                {
                    field: 'endTime',
                    title: '招聘结束时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        let  approvalFlag = row.status == 'WAITING_FOR_APPROVAL';
                        let failFlag = row.status == 'APPROVAL_FAILED';
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="details(\'' + row.postId + '\')"><i class="fa fa-search"></i>岗位详情</a> ');
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="detailsStu(\'' + row.postId + '\')"><i class="fa fa-search"></i>岗位学生</a> ');
                        if (approvalFlag){
                            actions.push(' <a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="publish(\'' + row.postId + '\')"><i class="fa fa-upload"></i>发布</a>');
                            actions.push(' <a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.postId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push(' <a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.postId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        }else if( failFlag){
                            actions.push(' <a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="record(\'' + row.postId + '\')"><i class="fa fa-search"></i>查看审批记录</a>');
                            actions.push(' <a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.postId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push(' <a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="rePublish(\'' + row.postId + '\')"><i class="fa fa-upload"></i>重新发布</a>');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        function details(id) {
            $.modal.openTab("详情",prefix+"/details/"+id)
        }
        function publish(postId) {
            parent.layer.open({
                type: 2,
                area: ['800px','500px'],
                fix: true,
                maxmin: true,
                shade: 0.3,
                shadeClose:true,
                title: '选择审核人',
                content: prefix + "/examineUsers/"+roleKey,
                btn: ['提交','取消']
                ,success:function (layero, index){    //弹出层加载成功后执行
                    let iframeWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                }
                ,yes: function(index, layero){
                    let iframeWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                    let examineUserId = iframeWin.submit();
                    let params = {
                        postId:postId,
                        "params[exmineUserId]":examineUserId
                    }
                    $.operate.save(prefixProcess+"/submit",params,function () {
                        if (examineUserId!='0'){
                            let params2 = {
                                "receiveUserId":examineUserId,
                                "status":"no",
                                "content":"消息内容：有新的发布岗位申请需要审核！",
                                "type":"消息提醒"
                            }
                            $.operate.post( "/wstudy/message/send",params2)
                        }
                    });
                }
                ,cancel: function(){

                }
            });
        }
        function rePublish(postId) {
            let exmineUserId
            let params1 = {
                postId:postId
            }
            $.ajax({
                url: prefixPE+"/query",
                type: 'post',
                cache: false,
                async : false,
                data: params1,
                dataType: "json",
                success: function (result) {
                    exmineUserId=result.data.exmineUserId
                    let params = {
                        postId:postId,
                        "params[exmineUserId]":exmineUserId
                    }
                    $.operate.save(prefixProcess+"/submit",params);
                }
            });

        }

        function record(postId) {
            parent.layer.open({
                type: 2,
                area: ['800px','500px'],
                fix: true,
                maxmin: true,
                shade: 0.3,
                shadeClose:true,
                title: '审核记录',
                content: prefixRecord+"/examine/"+postId,
                btn: ['提交','取消']
                ,success:function (layero, index){    //弹出层加载成功后执行
                    let iframeWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                }
                ,yes: function(index, layero){
                    let iframeWin = parent.window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                    let examineUserId = iframeWin.submit();
                    let params = {
                        postId:postId,
                        "params[exmineUserId]":examineUserId
                    }
                    $.operate.post(prefixProcess+"/submit",params);
                }
                ,cancel: function(){

                }
            });
        }
        function detailsStu(postId) {
            let params = {
                "postId":postId,
                "applyStatus":"FINISHED",
                "status":"ON"
            }
            $.modal.openTab("招收学生详情",prefixStu+"/postStuPage/"+postId)
        }
        function postFinished() {
            var row=$("#bootstrap-table").bootstrapTable('getSelections');
            let url ='/wstudy/process/finished';
            $.modal.confirm("确定完成此流程吗？",function () {
                $.each(row,function (index,item) {
                    let params = {
                        'postId':item.postId,
                        "postStatus":'FINISHED',
                        'status':'FINISHED'
                    };
                    $.ajax({
                        url:url,
                        data:params,
                        type:'post',
                        dataType: 'json',
                        async:'false',
                        success:function (result) {
                            $.table.refresh();
                        }
                    })
                })
            });
        }
    </script>
</body>
</html>